Environment Variables
Learn how to add environment variables to Vercel projects for use in v0 chats
v0 uses your Vercel project's environment variables when generating code. The setup is:
- Ensure the chat has a Vercel project attached.
- Add environment variables using the Vercel API.
- v0 automatically uses those variables when generating code.
1. Ensure the chat has a Vercel project
Environment variables are stored on Vercel projects. Before adding them, make sure the chat has a vercelProjectId.
import { v0 } from 'v0-sdk'const chat = await v0.chats.get({ chatId: 'chat_123',})let vercelProjectId = chat.vercelProjectIdif (!vercelProjectId) { const result = await v0.chats.createVercelProject({ chatId: 'chat_123', }) vercelProjectId = result.vercelProjectId}For more details, see Create Vercel Project.
2. Add environment variables with the Vercel API
Use the Vercel API to add environment variables to the project. This step happens outside the v0 Platform API.
// Add environment variables to the Vercel project
await fetch(
`https://api.vercel.com/v10/projects/${vercelProjectId}/env`,
{
method: "POST",
headers: {
Authorization: `Bearer ${process.env.VERCEL_TOKEN}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
key: "DATABASE_URL",
value: "postgresql://user:pass@host:5432/db",
type: "encrypted",
target: ["production", "preview", "development"],
}),
}
)For the full Vercel API reference, see Environment Variables.
Sensitive variables: v0 runs in the development environment. Vercel does not expose sensitive environment variables to the development environment, so avoid using the sensitive type for variables that v0 needs to access.
3. v0 uses the variables automatically
Once environment variables are added to the Vercel project, v0 can access them when generating code in that chat. The assistant sees which variables are available and generates code that references them.
For example, if you add DATABASE_URL and then prompt v0 to build a database-connected app, the generated code will use process.env.DATABASE_URL automatically.
When changes take effect: v0 reads environment variables from the Vercel project when it generates code. After adding, updating, or removing variables, sending a message will make the changes take effect in the chat and its preview.
import { v0 } from 'v0-sdk'// After adding DATABASE_URL to the Vercel project...await v0.chats.messages.send({ chatId: 'chat_123', prompt: 'Add a Postgres database connection using the DATABASE_URL',})// v0 generates code that uses process.env.DATABASE_URL